<?php

use Core\Container;

if (!function_exists('app')) {
    function app($abstract = null, $parameters = [])
    {
        $container = Container::getInstance();
        if (is_null($abstract)) {
            return $container;
        }
        return $container->make($abstract, $parameters);
    }
}

if (!function_exists('config')) {
    function config($key = null, $default = null)
    {
        $config = app()->make('config');
        if (is_null($key)) {
            return $config;
        }
        return $config[$key] ?? $default;
    }
}

if (!function_exists('dd')) {
    function dd(...$args)
    {
        foreach ($args as $arg) {
            echo "<pre style='background:#000;color:#0f0;padding:10px;border-radius:5px;'>";
            var_dump($arg);
            echo "</pre>";
        }
        die;
    }
}

if (!function_exists('jsonResponse')) {
    function jsonResponse($data, $status = 200, $headers = [])
    {
        http_response_code($status);
        header('Content-Type: application/json');
        foreach ($headers as $key => $value) {
            header("$key: $value");
        }
        if (is_string($data)) {
            $data = ['message' => $data];
        }
        echo json_encode($data);
        exit;
    }
}
